<?php
class ModelMailEmails extends Model
{
    public function getEmailsToOrderNotify()
    {
        $emails=array();

        if($this->config->get('config_alert_mail')) $emails=$this->getUsersNotifyEmails('is_alert_notificate');
        if($this->config->get('config_alert_mail_groups')) $emails=array_merge($emails, $this->getGroupsNotifyEmails('is_alert_notificate'));

        return $emails;
    }

    public function getEmailsToAccountNotify()
    {
        $emails=array();

        if($this->config->get('config_account_mail')) $emails=$this->getUsersNotifyEmails('is_account_notificate');
        if($this->config->get('config_account_mail_groups')) $emails=array_merge($emails, $this->getGroupsNotifyEmails('is_account_notificate'));

        return $emails;
    }

    private function getUsersNotifyEmails($attribute)
    {
        $usersToEmailNotifyModel=$this->model->load('usersToEmailNotifyModel');
        $userModel=$this->model->load('userModel');

        $emails=array();
        if($users_to_notify=$usersToEmailNotifyModel->findAllByAttributes(array($attribute=>1))){
            foreach($users_to_notify as $user){
                $notify_user=$userModel->findByPk($user['user_id']);
                $emails[]=$notify_user['email'];
            }
        }
        return $emails;
    }

    private function getGroupsNotifyEmails($attribute)
    {
        $userGroupToEmailNotifyModel=$this->model->load('userGroupToEmailNotifyModel');
        $userGroupModel=$this->model->load('userGroupModel');

        $emails=array();
        if($groups_to_notify=$userGroupToEmailNotifyModel->findAllByAttributes(array($attribute=>1))){
            foreach($groups_to_notify as $group){
                $notify_group=$userGroupModel->findByPk($group['user_group_id']);
                $emails[]=$notify_group['email'];
            }
        }
        return $emails;
    }
}